import Vue from 'vue'
import axios from 'axios';
import router from 'router';
import { Notification } from "element-ui";
import { ACCESS_TOKEN } from "@/store/mutation_types";
const httpRequest = axios.create({
  timeout: 50000
})
// 响应器
httpRequest.interceptors.request.use(
  config => {
    if (config.url.indexOf('/api') != 0) {
      config.headers.common['Authorization'] = "Bearer " + Vue.ls.get(ACCESS_TOKEN)
    }
    return config;
  },
  error => {
    return Promise.reject(error)
  }
)
// 拦截器
httpRequest.interceptors.response.use(
  response => {
    if (response.data.message == 'jwt expired') {
      router.push("/");
      Notification.success({
        title: "token验证过期",
        message: `token过期，请重新登录！`,
      });
    }
    if (response.data && response.status !== 200) {
      this.$message.error(response.message)
    }
    return response
  },
  error => {
    return error
  }

)
export default httpRequest